import cv2
if __name__=='__main__':
    image=cv2.imread("C:\\Users\\20436\Desktop\\tubao.jpg")
    #灰度化
    image_gray=cv2.cvtColor(
        image,
        cv2.COLOR_BGR2GRAY
    )
    #二值化
    _,image_thresh=cv2.threshold(
        image_gray,
        127,
        255,
        cv2.THRESH_BINARY_INV
    )
    _, image_thresh1 = cv2.threshold(
        image_gray,
        195,
        255,
        cv2.THRESH_BINARY
    )
    #找轮廓
    contours,hierarchy=cv2.findContours(
        image_thresh,
        cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE
    )
    contours1, hierarchy1 = cv2.findContours(
        image_thresh1,
        cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE
    )
    hull=cv2.convexHull(contours[0])
    hull1 = cv2.convexHull(contours[1])
    hull2 = cv2.convexHull(contours[2])
    hull3 = cv2.convexHull(contours1[0])
    hull4 = cv2.convexHull(contours1[1])
    cv2.polylines(
        image,
        [hull],
        isClosed=True,
        color=(0,0,255),
        thickness=2
     )
    cv2.polylines(
        image,
        [hull1],
        isClosed=True,
        color=(22,34,25),
        thickness=2
     )
    cv2.polylines(
        image,
        [hull2],
        isClosed=True,
        color=(0,255,255),
        thickness=2
     )

    cv2.polylines(
        image,
        [hull3],
        isClosed=True,
        color=(255, 0, 255),
        thickness=2
    )
    cv2.polylines(
        image,
        [hull4],
        isClosed=True,
        color=(24, 127, 55),
        thickness=2
    )

    cv2.imshow('image_thresh', image_thresh)
    cv2.imshow('image_thresh1',image_thresh1)
    cv2.imshow('image', image)
    cv2.imwrite('tubao.jpg', image)

    cv2.waitKey(0)